package com.dldata.devtools30.service.api;


import com.dldata.devtools30.entity.DevtApiMainEntity;
import com.dldata.devtools30.repository.api.DevtApiMainEntityDao;
import com.dldata.devtools30.utils.StringUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by ai on 2017/1/11.
 */
@Component
@Transactional
public class DevtApiMainEntityService {
    
    @Autowired
    DevtApiMainEntityDao devtApiMainEntityDao;
    
    public List<DevtApiMainEntity> getAllDataById(String flid) {
        return devtApiMainEntityDao.getAllDataById(flid);
    }
    public List<DevtApiMainEntity> getAllData() {
        return Lists.newArrayList(devtApiMainEntityDao.findAll());
    }

    public DevtApiMainEntity findOne(String id){
        return devtApiMainEntityDao.findById(id).get();
    }

    public DevtApiMainEntity save(DevtApiMainEntity sysKs){
//        return  pFunctionMainDao.save(sysKs.getId(),sysKs);
        return  devtApiMainEntityDao.save(sysKs);
    }

    public void save(List<DevtApiMainEntity> sysKs){
//        Map<String,DevtApiMainEntity> ids = new HashMap<>();
//        sysKs.forEach(e -> {
//            ids.put(e.getId(), e);
//        });
//        pFunctionMainDao.save(ids);
         devtApiMainEntityDao.saveAll(sysKs);
    }

    public void delete(String id){
        devtApiMainEntityDao.deleteById(id);
    }

    public List<DevtApiMainEntity> findByCondition(String condition,String flid) {
            if ((!StringUtil.isNullOrSpace(condition))&&(!StringUtil.isNullOrSpace(flid))){
                return devtApiMainEntityDao.findByCondition(condition,flid);
            }else {
                if(!StringUtil.isNullOrSpace(flid)&&StringUtil.isNullOrSpace(condition)){
                    return devtApiMainEntityDao.getAllDataById(flid);
                }
                if(!StringUtil.isNullOrSpace(condition)&&StringUtil.isNullOrSpace(flid)){
                    return devtApiMainEntityDao.findByCondition(condition);
                }
            }
        return  Lists.newArrayList(devtApiMainEntityDao.findAll());
    }

    /**
     * 功能类型枚举
     */
    public enum FunctionType{
//        BIAODAN(0,"表单"),
//        DELETE(1,"删除"),
//        SHENHE(2,"审核"),
//        YANSHOU(3,"验收"),
//        UPDATE(4,"修改"),
        DATALIST(5,"查询数据集"),
        POSTJSON(6,"更新数据库")
        ;

        private FunctionType( int value,String name){
            this.value=value;
            this.name=name;
        }

        public static FunctionType valueOf(int value){
            switch (value){
//                case 0:
//                    return FunctionType.BIAODAN;
//                case 1:
//                    return FunctionType.DELETE;
//                case 2:
//                    return FunctionType.SHENHE;
//                case 3:
//                    return FunctionType.YANSHOU;
//                case 4:
//                    return FunctionType.UPDATE;
                case 5:
                    return FunctionType.DATALIST;
                case 6:
                    return FunctionType.POSTJSON;
                default:
                    return FunctionType.DATALIST;
            }
        }

        private int value;

        private String name;

        @Override
        public String toString() {
            return value+" , "+name;
        }

        public int getValue() {
            return value;
        }

        public String getName() {
            return name;
        }
    }
    /**
     * 返回值类型枚举
     */
    public static enum FunctionResultType{
        json(0,"json"),
//        dhtml(1,"dhtml"),
//        easyui(2,"easyui"),
//        xml(3,"xml"),
        jsonmixcase(4,"jsonmixcase");

        private FunctionResultType( int value, String name){
            this.value=value;
            this.name=name;
        }
        public static FunctionResultType valueOf(int value){
            switch (value){
                case 0:
                    return FunctionResultType.json;
//                case 1:
//                    return FunctionResultType.dhtml;
//                case 2:
//                    return FunctionResultType.easyui;
//                case 3:
//                    return FunctionResultType.xml;
                case 4:
                    return FunctionResultType.jsonmixcase;

                default:
                    return FunctionResultType.json;
            }
        }
        private int value;

        private String name;

        public int getValue() {
            return value;
        }

        public String getName() {
            return name;
        }
    }
}
